////////////////////////////////////////////////////////////////////////////////
//
//  This program is free software; you can redistribute it and/or modify 
//  it under the terms of the GNU General Public License as published by the 
//  Free Software Foundation; either version 3 of the License, or (at your 
//  option) any later version.
//
//  This program is distributed in the hope that it will be useful, but 
//  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
//  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
//  for more details.
//
//  You should have received a copy of the GNU General Public License along 
//  with this program; if not, see <http://www.gnu.org/licenses>.
//
////////////////////////////////////////////////////////////////////////////////

package merapi.gps.parser;

import java.util.ArrayList;

import merapi.gps.model.GPSInfo;

/**
 *  A simple NMEA parser that extracts only latitude / longitude data from the stream.
 *  
 *  @author Adam Flater - adamflater.net
 */
public class SimpleNMEAParser implements IGPSParser 
{
    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Constructor.
     */
	public SimpleNMEAParser()
	{
		__buffer = new StringBuilder();
		__gpsInfoList = new ArrayList<GPSInfo>();
	}
	
    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------
	
	/**
	 *  Appends a new character to the parser buffer.
	 */
	public void append( char chr ) 
	{
		__buffer.append( chr );
		if ( chr == '\n' )
		{
			parseSentence( __buffer.toString() );
			__buffer = new StringBuilder();
		}
	}

	/**
	 *  Returns true if there are new IGPSInfo instances to be read.
	 */
	public boolean hasMoreElements() 
	{
		return __gpsInfoList.size() > 0;
	}

	/**
	 *  Returns the oldest parsed IGPSInfo instance.
	 */
	public GPSInfo nextGPSInfo() 
	{
		if ( __gpsInfoList.size() > 0 )
		{
			return __gpsInfoList.remove( 0 );
		}
		return null;
	}
	
	/**
	 *  @protected
	 *  
	 *  Parses an NMEA sentence into an IGPSInfo instance
	 */
	protected void parseSentence( String sentence ) 
	{
		String[] words = sentence.split( "," );
	
		//System.out.print( sentence );
		
		int latIdx = -1;
		int lngIdx = -1;
		int latDirIdx = -1;
		int lngDirIdx = -1;
		
		if ( words[ 0 ] != null && words[ 0 ].equals( "$GPRMC" ) && 
			 words[ 3 ] != null && !words[ 3 ].equals( "" ) && 
			 words[ 5 ] != null && !words[ 5 ].equals( "" ) )
		{
			latIdx = 3;
			latDirIdx = 4;
			lngIdx = 5;
			lngDirIdx = 6;
		}
		else if ( words[ 0 ] != null && words[ 0 ].equals( "$GPGGA" ) && 
				  words[ 2 ] != null && !words[ 2 ].equals( "" ) && 
				  words[ 4 ] != null && !words[ 4 ].equals( "" ) )
		{	
			latIdx = 2;
			latDirIdx = 3;
			lngIdx = 4;
			lngDirIdx = 5;
		}
		
		if ( latIdx != -1 && lngIdx != -1 )
		{
			double lat = parseLatOrLongVal( words[ latIdx ], words[ latDirIdx ].equals( "N" ) );
			double lng = parseLatOrLongVal( words[ lngIdx ], words[ lngDirIdx ].equals( "E" ) );

			if ( lat != -1 && lng != -1 )
			{
				GPSInfo gpsInfo = new GPSInfo();
				gpsInfo.setLatitude( lat );
				gpsInfo.setLongitude( lng );
				__gpsInfoList.add( gpsInfo );
			}
		}
	}
	
	/**
	 *  @protected
	 *  
	 *  Parses a String value (either lattitude or longitude) from an NMEA stream
	 *  into a double value
	 */
	protected double parseLatOrLongVal( String string, boolean positive )
	{
		try
		{
			double val = Double.parseDouble( string );
			double valPost = ( ( ( val / 100 ) - Math.round( val / 100 ) ) * 100 ) / 60;
			val = Math.round( val / 100 ) + valPost;
			
			if ( positive == false ) 
				val *= -1;	
			
			val = Math.round( val * 100000000.0 ) / 100000000.0;
	
			return val;
		}
		catch ( NumberFormatException ex )
		{
			// swallow for now
		}
		
		return -1;
	}	

	
    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------
	
	/**
	 *  @protected
	 *  
	 *  The buffer used to read from the gps hardware InputStream
	 */
	protected StringBuilder __buffer = null;
	
	/**
	 *  @protected
	 *  
	 *  The list of unprocessed IGPSInfo instances that have been parsed from the NMEA stream
	 */
	protected ArrayList<GPSInfo> __gpsInfoList = null;

}
